package Thread;

import java.util.concurrent.*;

class MyThread2 implements Runnable {
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(i);
        }
    }
}

public class CreateThread7 {
    public static void main(String[] args) {
        MyThread2 myThread = new MyThread2();

        //创建含有4个核心线程的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 4; i++) {
            //向线程池中提交任务
            executorService.submit(myThread);
        }

        //关闭线程池
        executorService.shutdown();
    }
}
//
//public class CreateThread7 {
//
//    public static void main(String[] args) {
//        // 创建一个线程池，大小为 4
//        ExecutorService executorService = Executors.newFixedThreadPool(4);
//
//        // 提交多个任务
//        for (int i = 0; i < 10; i++) {
//            executorService.submit(new RunnableTask(i));
//        }
//
//        // 关闭线程池
//        executorService.shutdown();
//    }
//}
//
//class RunnableTask implements Runnable {
//    private int taskId;
//
//    public RunnableTask(int taskId) {
//        this.taskId = taskId;
//    }
//
//    @Override
//    public void run() {
//        // 执行任务
//        System.out.println("执行任务 #" + taskId + " by " + Thread.currentThread().getName());
//        try {
//            // 模拟任务执行的延迟
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            Thread.currentThread().interrupt();
//        }
//        System.out.println("任务 #" + taskId + " 完成");
//    }
//}
